//
//  PersonViewController.m
//  动漫之家
//
//  Created by 王少泽 on 16/12/1.
//  Copyright © 2016年 王少泽. All rights reserved.
//

#import "PersonViewController.h"
//导入个人编辑页面与个人设置页面
#import "PersonSettingViewController.h"
#import "EditPersonViewController.h"


@interface PersonViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView* personalTableView;
@property (nonatomic,strong)UISegmentedControl* switchSegment;
@property (nonatomic,strong)UIButton* exitBtn;
@property (nonatomic,strong)NSArray* dataSource;

@end

@implementation PersonViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = YES;
    self.personalTableView.dataSource = self;
    self.personalTableView.delegate = self;
    [self.view addSubview:self.personalTableView];
    self.dataSource = @[@"漫画订阅",@"浏览记录",@"漫画下载",@"漫画评论"];
}
#pragma mark 懒加载模式
- (UITableView *)personalTableView
{
    if (_personalTableView == nil) {
        self.personalTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    }
    return _personalTableView;
}
- (UISegmentedControl *)switchSegment
{
    if (_switchSegment == nil) {
        NSArray* readType = @[@"我的漫画",@"我的小说"];
        self.switchSegment = [[UISegmentedControl alloc]initWithItems:readType];
        self.switchSegment.selectedSegmentIndex = 0;
    }
    return _switchSegment;
}

//返回页脚视图的的内容
- (UIView* )tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    self.exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.exitBtn.frame = CGRectMake(40, 20, 240, 40);
    [self.exitBtn setTintColor:[UIColor whiteColor]];
    [self.exitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    self.exitBtn.backgroundColor = [UIColor redColor];
    self.exitBtn.layer.masksToBounds = YES;
    self.exitBtn.layer.cornerRadius = 5;
    UIView* footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    [footView addSubview:self.exitBtn];
    return footView;

}


#pragma mark 返回tableview的头视图内容

- (UIView* )tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
//    headView.layer.masksToBounds = YES;
    self.switchSegment.frame = CGRectMake(40, 200, 240, 30);
    self.switchSegment.layer.masksToBounds = YES;
    [self.switchSegment addTarget:self action:@selector(switchReadType:) forControlEvents:UIControlEventValueChanged];
    [headView addSubview:self.switchSegment];
    
    
    //按钮与背景图
    UIImageView* bakcGroundImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 190)];
    bakcGroundImage.image = [UIImage imageNamed:@"personBack.jpg"];
    //按钮
    UIButton* personInfo = [UIButton buttonWithType:UIButtonTypeCustom];
    [personInfo setBackgroundImage:[UIImage imageNamed:@"testtouxiang.jpg"] forState:UIControlStateNormal];
    personInfo.layer.masksToBounds = YES;
    personInfo.layer.cornerRadius = 32;
    personInfo.frame = CGRectMake(self.view.frame.size.width / 2 - 32 , 85- 44, 64, 64);
    
    [personInfo addTarget:self action:@selector(personEdit:) forControlEvents:UIControlEventTouchUpInside];
    //添加设置的齿轮按钮
    UIButton* setting = [UIButton buttonWithType:UIButtonTypeCustom];
    setting.layer.masksToBounds = YES;
    setting.layer.cornerRadius = 8;
    [setting setBackgroundImage:[UIImage imageNamed:@"Setting"] forState:UIControlStateNormal];
    setting.frame = CGRectMake(self.view.frame.size.width - 40, 30, 16, 16);
    [bakcGroundImage addSubview:setting];
    //添加点击方法
    [setting addTarget:self action:@selector(personSetting:) forControlEvents:UIControlEventTouchUpInside];
    
    [bakcGroundImage addSubview:personInfo];
    bakcGroundImage.userInteractionEnabled = YES;
    
    
    
    
    [headView addSubview:bakcGroundImage];
    
    
    return headView;
}

//根据switch按钮的选择内容来进行判断内容更换

- (void)switchReadType:(UISegmentedControl* )sender
{
    if (sender.selectedSegmentIndex == 0) {
        self.dataSource = @[@"漫画订阅",@"浏览记录",@"漫画下载",@"漫画评论"];
        

    }
    if (sender.selectedSegmentIndex == 1) {
        self.dataSource = @[@"小说订阅",@"浏览记录",@"小说下载",@"小说评论"];
    }
    [_personalTableView reloadData];
    
    
}

#pragma mark tableview的代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* ruse = @"ruse";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ruse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ruse];
    }
    
    cell.textLabel.text = self.dataSource[indexPath.row];
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 240;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


#pragma mark 编辑个人信息与编辑个人偏好设置的按钮方法
//个人偏好设置
- (void)personSetting: (UIButton* )btn
{
    NSLog(@"123");
    PersonSettingViewController* personVC = [[PersonSettingViewController alloc]init];
    [self.navigationController pushViewController:personVC animated:YES];
    
}
//编辑个人资料
- (void)personEdit: (UIButton* )ben
{
    EditPersonViewController* eidtVC = [[EditPersonViewController alloc]init];
    [self.navigationController pushViewController:eidtVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
